package com.hksj.common.core.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

@Slf4j
public class RSAUtils {
    @Value("${rsa.public}")
    private String publicSecurity;
    public static final String ENCRYPT_TYPE = "RSA";

    /**
     * 公钥加密
     * @param content 要加密的内容
     * @param publicKey 公钥
     */
    public static String encrypt(String content, String publicKey) {
        try{
            RSA rsa=new RSA(null,publicKey);
            return rsa.encryptBase64(content, KeyType.PublicKey);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     * @param content 要解密的内容
     * @param privateKey 私钥
     */
    public static String decrypt(String content, String privateKey) {
        try {
            RSA rsa=new RSA(privateKey,null);
            return rsa.decryptStr(content, KeyType.PrivateKey);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成公钥私钥
     */
    public static void main(String[] args) {
        try {
            KeyPair pair= SecureUtil.generateKeyPair(ENCRYPT_TYPE);
            PrivateKey privateKey=pair.getPrivate();
            PublicKey publicKey=pair.getPublic();
            // 获取 公钥和私钥 的 编码格式（通过该 编码格式 可以反过来 生成公钥和私钥对象）
            byte[] pubEncBytes=publicKey.getEncoded();
            byte[] priEncBytes=privateKey.getEncoded();
//            // 把公钥和私钥的编码格式转换为Base64文本
//            String pubEncBase64=Base64.encode(pubEncBytes);
//            String priEncBase64=Base64.encode(priEncBytes);
//            log.info("公钥: {}", pubEncBase64);
//            log.info("私钥: {}", priEncBase64);
            String s="q4iyRZNBXsiuYk9RUL2JGJBz1rmWIvAqOORiEwh/BqyUtYmhosO7UxP6m25VHnd8VxQ13laUTisiSuih06DbupS+uaHNMuPAFHH7t+BRuFzxzOpdO8hrasVYfL/WF8XX4f5aJQCx4xPCBUdTZVlY2BnKf0L3EaqIASFjoA+bqx0=";
            String x="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAK9LcMum6YHRc44eLmjrhS5QDBDVVeHArVJntGMsD2jR/8TBojVM5Nj11kaoZkaAmUYWSz6gUujVBcIPXvmKrb33PnMoEQxFUhDp9F77eZd+X2m2hLvQXAbytfFQ3keSRuvbpLR5YzggzUrGg2Sizo8L/LTZQ88f1+lNkoUjo2sVAgMBAAECgYAEnVzPPWWBKGRGhfEqi+5uoK/8DprWnZkyfJeQHDnUMisSRamPGDmNf3xwLS5y80QDa+ccoRgkhdzVqEyBWGX6wrE8JqaIp4X61NXU+BOt10dlRtaj74teICcwVTXiBSP8eZhKhukuguSU2dV0WxalTnIGGZJg6A4NADXrTPWkJQJBANacJ7kd2v8dsJiIAMxCN6xgfGKtSEfjv0HkaV1tvTNFw4yQqUUH8HqylO9hfka+mUwihN8jSOaGPqXDiWOVyhsCQQDRGjBRXOaRACgxnRu+j/hXWv1GisAiS9CMT/xZiUk1Av69UV7naT/236EGzWETOmboi2aewlvD1CC6aRNcXvKPAkEAwrM+8poccbE84tUYbDdzDOcoUdnFprr/bEdONB34NYkIZQFlICSYzqgvqJF5n7MRntn+U/ZOEB4gvgCNJ5TgmwJBAKB1W0ykYJ42o6Fm8WpramQGg8NFKClpSlDR9FD1A8N1V+wJbC1iW95jP1tL8UljEKM0jfwYI3kD6FWD2oKQrcsCQHLj9iV9noOOLC0Wv5GcspLgG2CvjlHYo0vFiWScVoh5oOvHAaBEYxI9AQnrAUVjtEqdZH//wVNbqfReQXZth5I=";

            System.out.println("解密11111"+decrypt(s,x));
            //MD5加密
            String passWord = SecureUtil.md5("123456");
            String ss = SecureUtil.md5(passWord);
            System.out.println(ss);


//            String s1 = DigestUtils.md5Hex("123456");


            String admin = encrypt("123456", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvS3DLpumB0XOOHi5o64UuUAwQ1VXhwK1SZ7RjLA9o0f/EwaI1TOTY9dZGqGZGgJlGFks+oFLo1QXCD175iq299z5zKBEMRVIQ6fRe+3mXfl9ptoS70FwG8rXxUN5Hkkbr26S0eWM4IM1KxoNkos6PC/y02UPPH9fpTZKFI6NrFQIDAQAB");
            System.out.println(admin);

            String  z="RFcwuyfMOOGHgfFrmzxTmfMQ/AvMGpCG1WQSeafE6Lc04CepzTdfp5dlneJBVaMkV83OjG46MyZJgU6hUfp595+RwHYtxhiw9oJ1z4S5FFsIrDVWFAwL8r9Ll+G8VckShqFVcYAQUZPHWTutbH+SNn23vhwbnGZXJIhEnIQXBOI=";
            System.out.println("解密"+decrypt(z,x));
        } catch (Exception e){
            e.printStackTrace();
        }
    }


}